package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.ShowURLActivity;
import com.xm.xmapp.adapter.GridViewAdapter;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.exception.MyException;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ZFBInterFace;
import com.xm.xmapp.request.bean.Add_pay_orderRequest;
import com.xm.xmapp.request.bean.Get_ali_pay_signRequest;
import com.xm.xmapp.request.bean.Get_user_pay_privilegeRequest;
import com.xm.xmapp.request.bean.Get_weixin_pay_signRequest;
import com.xm.xmapp.request.bean.Zhima_AuthorizeRequest;
import com.xm.xmapp.response.bean.ResponseALIpaysign;
import com.xm.xmapp.response.bean.Get_user_pay_privilegeResponse;
import com.xm.xmapp.response.bean.ResponseWXpaysign;
import com.xm.xmapp.response.bean.ResponseAddPayOrder;
import com.xm.xmapp.response.bean.ResponseZhimaAuthorize;
import com.xm.xmapp.tools.PayUitl;
import com.xm.xmapp.tools.Tools;

public class WalletPayFragment extends BaseFragment implements OnClickListener {
	private TextView pay, text;
	private RelativeLayout rel1, rel2;
	private int flag;
	private double paymoney;
	private ImageView img1, img2;
	private TextView go;
	private GridView gridView;
	private GridViewAdapter adapter;
	private ArrayList<String[]> list = new ArrayList<String[]>();
	boolean isAuthorize;

	public WalletPayFragment(boolean isAuthorize) {
		this.isAuthorize = isAuthorize;
	}

	@Override
	public int setContentView() {
		return R.layout.fragment_pay;
	}

	@Override
	public void initView() {
		pay = (TextView) findViewById(R.id.pay);
		rel1 = (RelativeLayout) findViewById(R.id.rel1);
		rel2 = (RelativeLayout) findViewById(R.id.rel2);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		gridView = (GridView) findViewById(R.id.gridView);
		text = (TextView) findViewById(R.id.text);
		go = (TextView) findViewById(R.id.go);
		isSuppertLazyLoad = false;
	}

	@Override
	public void initData() {
		flag = 1;
		gridView.setVisibility(View.VISIBLE);
		text.setVisibility(View.VISIBLE);
		adapter = new GridViewAdapter(list, getActivity(), R.layout.item_privilege);
		gridView.setAdapter(adapter);
		adapter.setContent(getActivity());
		promptDialog.show();
		getData();

	}

	private void getData() {
		Get_user_pay_privilegeRequest requestBean = new Get_user_pay_privilegeRequest();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				Get_user_pay_privilegeResponse base = JSON.parseObject(response, Get_user_pay_privilegeResponse.class);
				setData(base);
			}

		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				setData(null);
			}
		});

	}

	/**
	 * 设置adaptpter数据
	 * 
	 * @param base
	 */
	private void setData(Get_user_pay_privilegeResponse base) {

		if (list != null) {
			list.clear();
		}
		if (base != null && base.is_privilege == 1) {
			list.addAll(base.privilege);
			if (base.end_date != null && !base.end_date.equals(""))
				text.setText("优惠截止日期:" + base.end_date);
		}
		// 截至日期：2016-6-25
		if (list.size() == 0) {
			String[] ss = new String[2];
			ss[0] = "100";
			list.add(ss);
		}
		if (list.size() == 1) {
			String[] ss = new String[2];
			ss[0] = "300";
			list.add(ss);
		}
		if (list.size() == 2) {
			String[] ss = new String[2];
			ss[0] = "500";
			list.add(ss);
		}
		String[] ss = new String[2];
		list.add(ss);
		adapter.notifyDataSetChanged();
	}

	/**
	 * 获取支付宝支付参数
	 * 
	 * @param pay_order_number
	 */
	private void get_ali_pay_signRequest(String pay_order_number) {
		Get_ali_pay_signRequest requestBean = new Get_ali_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				pay.setClickable(true);
				promptDialog.dismiss();
				try {
					ResponseALIpaysign base = JSON.parseObject(response, ResponseALIpaysign.class);
					PayUitl.getInstence().zfb(getActivity(), new ZFBInterFace() {
						@Override
						public void success() {
							PayUitl.getInstence().afterPayMoney();
						}
					}, base.sign_string);
				} catch (Exception e) {
					MyException.printStackTrace(e);
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initEvent() {
		pay.setOnClickListener(this);
		rel1.setOnClickListener(this);
		rel2.setOnClickListener(this);
		go.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		//
		switch (v.getId()) {
		case R.id.go:
			promptDialog.show();
			Zhima_AuthorizeRequest requestBean = new Zhima_AuthorizeRequest();
			MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {
					promptDialog.dismiss();
					ResponseZhimaAuthorize bean = JSON.parseObject(response, ResponseZhimaAuthorize.class);
					Intent intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
					intent.putExtra("url", bean.authorize_url);
					intent.putExtra("title", "芝麻信用");
					startActivity(intent);
					getActivity().finish();
				}
			}, new DisssmissInterFace() {

				@Override
				public void putError(VolleyError error) {
					promptDialog.dismiss();
				}
			});
			break;

		case R.id.pay:
			if (list.get(adapter.getSelect())[0] == null || list.get(adapter.getSelect())[0].equals("")) {
				Tools.showMessage("请输入充值金额!");
			} else {
				try {
					paymoney = Double.parseDouble(list.get(adapter.getSelect())[0]);
					add_pay_orderRequest();
					pay.setClickable(false);
				} catch (Exception e) {
					Tools.showMessage("请输入充值金额!");
				}
			}
			break;
		case R.id.rel1:
			flag = 1;
			setImg();
			break;
		case R.id.rel2:
			flag = 2;
			setImg();
			break;
		default:
			break;
		}
	}

	private void setImg() {
		if (flag == 1) {
			img1.setImageResource(R.mipmap.icon_xuanze_nor);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		} else {
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_nor);
		}
	}

	private void add_pay_orderRequest() {
		Add_pay_orderRequest requestBean = new Add_pay_orderRequest();
		requestBean.type = 1;// 1=钱包充值，2=押金（后续参数再补）
		requestBean.price = paymoney;
		if (flag == 1) {
			requestBean.payType = 2;
		} else {
			requestBean.payType = 6;
		}
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				try {
					ResponseAddPayOrder base = JSON.parseObject(response, ResponseAddPayOrder.class);
					if (flag == 1) {
						get_ali_pay_signRequest(base.pay_order_number);
					} else {
						get_weixin_pay_signRequest(base.pay_order_number);
					}
				} catch (Exception e) {
					e.printStackTrace();
					promptDialog.dismiss();
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				if (promptDialog != null)
					promptDialog.dismiss();
				pay.setClickable(true);
			}
		});

	}

	@Override
	public void onPause() {
		super.onPause();
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
	}

	protected void get_weixin_pay_signRequest(String pay_order_number) {
		Get_weixin_pay_signRequest requestBean = new Get_weixin_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				pay.setClickable(true);
				try {
					JSONObject obj = JSONObject.parseObject(response);
					ResponseWXpaysign wxBean = JSON.parseObject(response, ResponseWXpaysign.class);
					wxBean.package_str = obj.getString("package");
					PayUitl.getInstence().genWxPay(getActivity(), wxBean, promptDialog);
				} catch (Exception e) {
					MyException.printStackTrace(e);
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

}
